﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Concurrent;
namespace HB.Base
{
    /// <summary>
    /// 环形队列
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class CircleQueue<T>
    {
        Queue<T> queue;
        int len = 0;
        /// <summary>
        /// 按照指定长度初始化队列
        /// </summary>
        /// <param name="len"></param>
        public CircleQueue(int len)
        {
            queue = new Queue<T>(len);
            this.len = len;

        }
        /// <summary>
        /// 按照指定列表生成队列
        /// </summary>
        /// <param name="len"></param>
        /// <param name="ls">初始化值</param>
        public CircleQueue(int len, List<T> ls)
        {
            queue = new Queue<T>(len);
            this.len = len;
            Enqueue(ls);
        }

        /// <summary>
        /// 为队列添加元素，自动覆盖旧数据
        /// </summary>
        /// <param name="value"></param>
        public void Enqueue(T value)
        {
            queue.Enqueue(value);
            if (queue.Count > len)
            {
                queue.Dequeue();
            }
        }
        /// <summary>
        /// 批量添加数据
        /// </summary>
        /// <param name="list"></param>
        public void Enqueue(IEnumerable<T> list)
        {
            foreach (var i in list)
            {
                Enqueue(i);
            }
        }

        /// <summary>
        /// 获取队列中的第一个值
        /// </summary>
        /// <returns></returns>
        public T Peek()
        {
            return queue.Peek();
        }
        /// <summary>
        /// 当前队列中的元素数量
        /// </summary>
        public int Count
        {
            get { return queue.Count; }
        }
        /// <summary>
        /// 清理队列
        /// </summary>
        public void Clear()
        {
            queue.Clear();
        }
        /// <summary>
        /// 将队列中已有的数据按照顺序转换为列表
        /// </summary>
        /// <returns></returns>
        public List<T> ToList()
        {
            return queue.ToList();
        }
    }
}
